import { defineStore } from 'pinia'
import { ref } from 'vue'
import { attendanceTypeApi } from '@/axios/api/attendance-types'
import { ElMessage } from 'element-plus'

// 定义考勤类型接口
export interface AttendanceType {
  type_id: number
  type_name: string
}

// 创建考勤类型Store
export const useAttendanceTypeStore = defineStore('attendanceType', () => {
  // 状态
  const attendanceTypes = ref<AttendanceType[]>([])
  const loading = ref(false)
  const currentType = ref<AttendanceType | null>(null)

  // 获取所有考勤类型
  const fetchAttendanceTypes = async () => {
    loading.value = true
    try {
      console.log('开始获取考勤类型列表')
      const response = await attendanceTypeApi.getAttendanceTypes() as unknown as AttendanceType[]
      console.log('获取考勤类型列表响应:', response)
      attendanceTypes.value = response
    } catch (error) {
      console.error('获取考勤类型失败:', error)
      ElMessage.error('获取考勤类型失败')
    } finally {
      loading.value = false
    }
  }

  // 获取单个考勤类型
  const fetchAttendanceType = async (id: number) => {
    loading.value = true
    try {
      const response = await attendanceTypeApi.getAttendanceTypes() as unknown as AttendanceType[]
      const type = response.find((t: AttendanceType) => t.type_id === id)
      if (type) {
        currentType.value = type
        return type
      }
      return null
    } catch (error) {
      console.error(`获取考勤类型 ${id} 失败:`, error)
      ElMessage.error(`获取考勤类型 ${id} 失败`)
      return null
    } finally {
      loading.value = false
    }
  }

  // 创建考勤类型
  const createAttendanceType = async (type: Omit<AttendanceType, 'type_id'> & { type_id?: number }) => {
    loading.value = true
    try {
      console.log('开始创建考勤类型:', type)
      const response = await attendanceTypeApi.createAttendanceType(type) as unknown as AttendanceType
      console.log('创建考勤类型响应:', response)
      ElMessage.success('创建考勤类型成功')
      // 不自动刷新列表，由调用者决定是否刷新
      // await fetchAttendanceTypes()
      return response
    } catch (error) {
      console.error('创建考勤类型失败:', error)
      ElMessage.error('创建考勤类型失败')
      return null
    } finally {
      loading.value = false
    }
  }

  // 批量创建考勤类型
  const batchCreateAttendanceTypes = async (types: Array<Omit<AttendanceType, 'type_id'> & { type_id: number }>) => {
    loading.value = true
    try {
      console.log('开始批量创建考勤类型，数量:', types.length)
      const response = await attendanceTypeApi.batchCreateAttendanceTypes(types) as unknown as AttendanceType[]
      console.log('批量创建考勤类型响应:', response)
      ElMessage.success(`成功创建 ${response.length} 个考勤类型`)
      // 不在这里刷新列表，由调用者决定是否刷新
      return response
    } catch (error) {
      console.error('批量创建考勤类型失败:', error)
      ElMessage.error('批量创建考勤类型失败')
      return null
    } finally {
      loading.value = false
    }
  }

  // 更新考勤类型
  const updateAttendanceType = async (id: number, type: Partial<AttendanceType>) => {
    loading.value = true
    try {
      console.log('开始更新考勤类型，ID:', id, '数据:', type)
      const response = await attendanceTypeApi.updateAttendanceType(id, type) as unknown as AttendanceType
      console.log('更新考勤类型响应:', response)
      ElMessage.success('更新考勤类型成功')
      // 不自动刷新列表，由调用者决定是否刷新
      // await fetchAttendanceTypes()
      return response
    } catch (error) {
      console.error(`更新考勤类型 ${id} 失败:`, error)
      ElMessage.error(`更新考勤类型 ${id} 失败`)
      return null
    } finally {
      loading.value = false
    }
  }

  // 删除考勤类型
  const deleteAttendanceType = async (id: number) => {
    loading.value = true
    try {
      console.log('开始删除考勤类型，ID:', id)
      await attendanceTypeApi.deleteAttendanceType(id)
      ElMessage.success('删除考勤类型成功')
      // 不自动刷新列表，由调用者决定是否刷新
      // await fetchAttendanceTypes()
      return true
    } catch (error) {
      console.error(`删除考勤类型 ${id} 失败:`, error)
      ElMessage.error(`删除考勤类型 ${id} 失败`)
      return false
    } finally {
      loading.value = false
    }
  }

  return {
    attendanceTypes,
    loading,
    currentType,
    fetchAttendanceTypes,
    fetchAttendanceType,
    createAttendanceType,
    batchCreateAttendanceTypes,
    updateAttendanceType,
    deleteAttendanceType
  }
}) 